//
//  MakeCardViewController.m
//  cherryPicker
//
//  Created by Young Soo Yun on 12. 1. 4..
//  Copyright (c) 2012 Happy. All rights reserved.
//

#import "MakeCardViewController.h"

#import "CherryPickerDB.h"
#import "StringUtils.h"
#import "ControlUtil.h"

@implementation MakeCardViewController

@synthesize _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _cardType = 0;
    _labTitle.text = (_isModify)?@"카드 수정":@"카드 생성";
    
    if (_isModify) {
        _unmodifyView.alpha = 0.5f;
        _btnDel.hidden = NO;
    }
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    _delegate = nil;
    
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    return YES;
}


#pragma mark - IBAction

- (IBAction)onOK{
    
    if (![StringUtils isCheckNotNull:_tfCardName.text]) {
        [ControlUtil showAlertCaution:@"" body:@"카드명을 입력해 주십시오." btn:@"확인" delegate:nil];
    }else if([_labPayDate.text isEqualToString:_labUseDate.text] && _cardType == 0){
        [ControlUtil showAlertCaution:@"" body:@"대금결제일과 이용기준일이 동일합니다. 확인해 주십시오." btn:@"확인" delegate:nil];
    }else{
        NSArray* arrPayDate = [_labPayDate.text componentsSeparatedByString:@" "];
        NSArray* arrUseDate = [_labUseDate.text componentsSeparatedByString:@" "];
        
        CardListDB* cardDB = [[CardListDB alloc] initWithCode:_tfCardName.text
                                                    delimiter:_labDelimiter.text
                                                      company:_labCompany.text
                                                     cardType:_cardType
                                                      payDate:[[arrPayDate objectAtIndex:0] intValue]
                                                      useDate:[[arrUseDate objectAtIndex:0] intValue]
                                                   limitPrice:[[_labPrice.text substringWithRange:NSMakeRange(0, _labPrice.text.length-3)] intValue]*10000];
        
        if (_isModify) {
            [CherryPickerDB updateToCardList:cardDB];
            [_delegate MakeCardOKClick];
        }else{
            BOOL isInsert = [CherryPickerDB insertFromCardList:cardDB];
            
            if (isInsert) {
                [_delegate MakeCardOKClick];
            }else{
                [ControlUtil showAlertCaution:@"" body:@"DB에 저장 실패하였습니다." btn:@"확인" delegate:nil];
            }

        }
    }
}

- (IBAction)onCancel{
    [_delegate MakeCardCancelClick];
}

- (IBAction)onPickerView:(id)sender{
    [_tfCardName resignFirstResponder];
    
    UIButton* btn = (UIButton*)sender;
    
    switch (btn.tag) {
        case TAG_DELIMITER:
            [_delegate MakeCardPickerView:_labDelimiter.text tag:btn.tag];
            break;
        case TAG_COMPANY:
            [_delegate MakeCardPickerView:_labCompany.text tag:btn.tag];
            break;
        case TAG_PAYDATE:
            if (_cardType == 0) {
                [_delegate MakeCardPickerView:_labPayDate.text tag:btn.tag];
            }
            break;
        case TAG_USEDATE:
            if (_cardType == 0) {
            [_delegate MakeCardPickerView:_labUseDate.text tag:btn.tag];
            }
            break;
        case TAG_PRICE:
            [_delegate MakeCardPickerView:_labPrice.text tag:btn.tag];
            break;
    }
}

- (IBAction)onSelectCardType:(id)sender{
    UIButton* btn = (UIButton*)sender;
    
    if (btn.tag == 1) {
        _cardType = 0;
        _dayView.alpha = 1.0f;
        [_btnCredit setImage:[UIImage imageNamed:@"t_l_sel.png"] forState:UIControlStateNormal];
        [_btnCheck setImage:[UIImage imageNamed:@"t_r_nor.png"] forState:UIControlStateNormal];
    }else{
        _cardType = 1;
        _dayView.alpha = 0.5f;
        [_btnCredit setImage:[UIImage imageNamed:@"t_l_nor.png"] forState:UIControlStateNormal];
        [_btnCheck setImage:[UIImage imageNamed:@"t_r_sel.png"] forState:UIControlStateNormal];
    }
}

- (IBAction)onCardDel{
    // delete card
    [CherryPickerDB deleteCardList:_labDelimiter.text];
    [CherryPickerDB deleteReceiptWhereCard:_labDelimiter.text];
    
    [_delegate MakeCardDelClick];
}

#pragma mark -

- (void)setLabel:(NSString *)labText
             tag:(NSInteger)tag{
    
    switch (tag) {
        case TAG_DELIMITER:
            _labDelimiter.text = labText;
            break;
        case TAG_COMPANY:
            _labCompany.text = labText;
            break;
        case TAG_PAYDATE:
            _labPayDate.text = labText;
            break;
        case TAG_USEDATE:
            _labUseDate.text = labText;
            break;
        case TAG_PRICE:
            _labPrice.text = labText;
            break;
        case TAG_TFCARDNAME:
            _tfCardName.text = labText;
            break;
        case TAG_CHECK:{
            if ([labText isEqualToString:@"1"]) {
                _cardType = 1;
                _dayView.alpha = 0.5f;
                [_btnCredit setImage:[UIImage imageNamed:@"t_l_nor.png"] forState:UIControlStateNormal];
                [_btnCheck setImage:[UIImage imageNamed:@"t_r_sel.png"] forState:UIControlStateNormal]; 
            }
        }
    }
}

- (void)initType:(MakeCardType)type{
    _isModify = (type == MODIFY_CARD)?YES:NO;
}

@end
